<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class RegisterForm extends CFormModel
{
    public $name;    public $email;    public $message;    public $enabledReigster;    public $enableMail;    public $password;
    public $confirm;
    
    public function init(){
        $this->enableMail = Yii::app()->params["app"]["enableMail"];
        $this->enabledReigster = Yii::app()->params["app"]["enabledReigster"];
        if($this->enableMail!="1"){            $this->addError("name", "系统不能发送邮件，请联系管理员");
        }
        if($this->enabledReigster!="1"){
            $this->addError("name", "系统未开放注册");
        }
    }
    public function rules()
    {
        return array(
        array('name,email', 'required'),
        array('email', 'email'),
        array('name', 'nameCheck'),
        array('name', 'emailCheck'),
        array('password','passwordCheck'),
        array('confirm','passwordCheck')
        );
    }
    
    public function passwordCheck()
    {
        if (strlen($this->password) < 6 || strlen($this->password) > 20){
            $this->addError("password", "密码长度为6-20字符");
        }
        if ($this->confirm != $this->password){
            $this->addError("confirm", "两次输入的密码不一致");
        }
        return true;
    }
    
    public function nameCheck(){
        $result = User::model()->count("user_name=?",array($this->name))==0?true:false;
        if($result==false){
            $this->addError("name", "用户已经存在，请更换一个哦");
        }
        return $result;
    }
    
    public function emailCheck(){
        $result = UserMeta::model()->count("meta_key='email' and meta_value=?",array($this->email))==0?true:false;
        if($result==false){
            $this->addError("email", "电子邮件已经存在，请更换一个哦");
        }
        return $result;
    }

    public function attributeLabels()
    {
        return array(
            'name'=>'用户名',
            'email'=>'电子邮件', 
		    'password'=>'密码',
		    'confirm'=>'确认密码'
		    );
    }
    
    private function _getMailTitle(){
        $title = "[".Yii::app()->params['app']['name']."] 您的用户名和密码";
        return $title;
    }
    
    private function _getMailContent($passwd){
        $body = "用户名:".$this->name."<br>";
        $body = $body."密码:".$passwd."<br>";
        $body = $body.Yii::app()->params['app']['host'];
        return $body;
    }
    public function save(){
        if($this->validate()){
            if($this->enabledReigster=="1"){
                                                
                                $salt = CUtils::genRandomString(6);
                $user = new User();
                $user->user_uuid = uniqid();
                $user->user_name = $this->name;
                $user->salt      = $salt;
                $user->user_pass = CSecurity::signPasswd($this->password, $salt);
                $user->user_status=1;
                $user->save();
                $userMeta = UserMeta::model()->find("user_id=?",array($user->id));
                if(!isset($userMeta)){
                    $userMeta = new UserMeta();
                }
                $userMeta->meta_key="email";
                $userMeta->meta_value=$this->email;
                $userMeta->user_id = $user->id;
                $userMeta->save();
                                $this->message = "注册完成。";
                                                                return true;
            }
            return false;
        }
    }

}
